import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: []
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("sign-in")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
                console.log(this.data.member);
            });
        await db.collection("controlSign-in")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = This.data.member;
                res.data.map((value) => {
                    value.control = true;
                    member.push(value);
                })
                This.setData({ member: member });

            });
    },
    async delete(e) {
        await Dialog.confirm({
                title: '提示',
                message: '请确认删除该成员信息',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    if (!e.currentTarget.dataset.control) {
                        db.collection('sign-in').where({
                            userName: e.target.dataset.username,
                            unionId: e.target.dataset.id
                        }).remove().then(res => {
                            if (res.deleted != 0) {
                                for (let i in this.data.member) {
                                    if (this.data.member[i].userName == e.target.dataset.username && this.data.member[i].unionId == e.target.dataset.id) {
                                        this.data.member.splice(i, 1);
                                        this.setData({ member: this.data.member });
                                    }
                                }
                                Toast('删除成功');
                            } else {
                                Toast('删除失败');
                            }
                        }).catch(err => {
                            Toast('删除失败请重新删除');
                        })
                        db.collection('black').add({
                                data: {
                                    userName: e.target.dataset.username,
                                    unionId: e.target.dataset.id,
                                    alliance: app.data.alliance
                                },
                            })
                            .then(res => {})
                            .catch(err => {});
                    } else {
                        db.collection('controlMember').where({
                            Name: e.target.dataset.username,
                            unionId: e.target.dataset.id
                        }).remove().then(res => {}).catch(err => {})
                        db.collection('controlSign-in').where({
                            userName: e.target.dataset.username,
                            unionId: e.target.dataset.id
                        }).remove().then(res => {
                            if (res.deleted != 0) {
                                for (let i in this.data.member) {
                                    if (this.data.member[i].userName == e.target.dataset.username && this.data.member[i].unionId == e.target.dataset.id) {
                                        this.data.member.splice(i, 1);
                                        this.setData({ member: this.data.member });
                                    }
                                }
                                Toast('删除成功');
                            } else {
                                Toast('删除失败');
                            }
                        }).catch(err => {
                            Toast('删除失败请重新删除');
                        })
                    }
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },


})